{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python3基础： 元组tuple、 列表list、 字典dict、集合set、迭代器、生成器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 的元组与列表类似，不同之处在于元组的元素不能修改。\n",
    "\n",
    "元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 遍历元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111\n",
      "22\n",
      "33\n",
      "444\n",
      "55\n",
      "6\n",
      "77\n"
     ]
    }
   ],
   "source": [
    "tup2 = (111, 22, 33, 444, 55, 6, 77 )\n",
    "for x in (tup2):      \n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将列表转变为元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(111, 22, 33, 444, 55, 6, 77)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list2 = [111, 22, 33, 444, 55, 6, 77 ]\n",
    "tup2 = tuple(list2)\n",
    "print(type(tup2))\n",
    "tup2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表：就是一系列按照特定顺序排列的元素,列表通过方括号[ ]来定义，用逗号来分隔其中的元素\n",
    "列表的特征：\n",
    "1. 列表中的每个元素都是可变化的——说明我们能够去修改列表元素\n",
    "2. 列表中的元素与位置是对应的（一个萝卜一个坑，一个元素对应一个位置）——说明我们能够去根据位置来查询列表元素，即索引\n",
    "3. 列表中的元素可以是任意的对象——包括字符串、数值、布尔值、列表、元组、字典、集合等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  获取列表长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1 = [1, 2, 3, 6, 5, 4]\n",
    "len(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 访问列表元素——索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 6, 5, 4]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "4\n",
      "[1, 2, 3, 6, 5, 4]\n",
      "[2, 3]\n",
      "[1, 2, 3, 6, 5]\n"
     ]
    }
   ],
   "source": [
    "print(list1[0]) #打印出list1中第1个元素\n",
    "print(list1[2]) #打印出list1中第3个元素\n",
    "print(list1[-1]) #打印出list1中最后一个元素\n",
    "print(list1[0:]) #打印出从第一个元素开始，到最后一个元素\n",
    "print(list1[1:3]) # 打印list1从第2个元素到第3个元素；\n",
    "print(list1[:5]) # 打印list1 代表从第1个字母取到第5个字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 遍历列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,2,3,6,5,4,"
     ]
    }
   ],
   "source": [
    "#遍历列表\n",
    "for x in list1:\n",
    "    print(x, end=\",\")  # 运行结果：1,2,3,6,5,4,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表+索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "序号： 0   值： 1\n",
      "序号： 1   值： 2\n",
      "序号： 2   值： 3\n",
      "序号： 3   值： 6\n",
      "序号： 4   值： 5\n",
      "序号： 5   值： 4\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(list1)):\n",
    "    print(\"序号：\", i, \"  值：\", list1[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "序号： 0   值： 1\n",
      "序号： 1   值： 2\n",
      "序号： 2   值： 3\n",
      "序号： 3   值： 6\n",
      "序号： 4   值： 5\n",
      "序号： 5   值： 4\n"
     ]
    }
   ],
   "source": [
    "for i, val in enumerate(list1):\n",
    "    print(\"序号：\", i, \"  值：\", val)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 --------- 2\n",
      "2 --------- 3\n",
      "3 --------- 6\n",
      "6 --------- 5\n",
      "5 --------- 4\n"
     ]
    }
   ],
   "source": [
    "for i in list1:\n",
    "    idx = list1.index(i) # 索引\n",
    "    if (idx < len(list1) - 1):\n",
    "        print(i, '---------', list1[idx + 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序列表、判断元素是否在列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "list1 = [1,2,3,6,5,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序列表（正序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,2,3,4,5,6,\n"
     ]
    }
   ],
   "source": [
    "#排序列表（正序）\n",
    "list1.sort()\n",
    "for x in list1:\n",
    "    print(x, end=\",\")   #运行结果：1,2,3,4,5,6,\n",
    "print(\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序列表（倒序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6,5,4,3,2,1,\n"
     ]
    }
   ],
   "source": [
    "#排序列表（倒序）\n",
    "list1.reverse()\n",
    "for x in list1:\n",
    "    print(x, end=\",\")   #运行结果：6,5,4,3,2,1,\n",
    "print(\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 判断元素是否存在于列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num = 20\n",
    "nums = [10, 20, 30, 40]\n",
    "num in nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 在list1中\n"
     ]
    }
   ],
   "source": [
    "if 5 in list1:\n",
    "    print(\"5 在list1中\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表末尾追加新的元素append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 6, 5, 4, 555, 666, 555]\n"
     ]
    }
   ],
   "source": [
    "list1.append(555)\n",
    "list1.append(666)\n",
    "list1.append(555)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 统计某个元素在列表中出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出现 2 次\n"
     ]
    }
   ],
   "source": [
    "print(\"出现\",list1.count(555),\"次\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 移除元素，并返回值（默认是移除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(list1.pop(0)) # 移除第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 6, 5, 4, 555, 666]\n"
     ]
    }
   ],
   "source": [
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "666\n"
     ]
    }
   ],
   "source": [
    "print(list1.pop()) # 移除最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 6, 5, 4, 555]\n"
     ]
    }
   ],
   "source": [
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表其他常用功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['jack', 'lucy', 'klaus']\n"
     ]
    }
   ],
   "source": [
    "#试着打印出列表\n",
    "name = [\"jack\",\"lucy\",\"klaus\"]\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['fizz', 'lucy', 'klaus']\n"
     ]
    }
   ],
   "source": [
    "#特征1：列表中的每个元素都是可变的(修改列表内容)\n",
    "name = [\"jack\",\"lucy\",\"klaus\"]\n",
    "name[0] = \"fizz\"\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lucy'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#特征2：列表元素与位置是对应的（索引从0开始）\n",
    "name = [\"jack\",\"lucy\",\"klaus\"]\n",
    "name[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 2.5,\n",
       " 'abc',\n",
       " True,\n",
       " False,\n",
       " [1, 2, 3],\n",
       " (2, 4, 6),\n",
       " {'color': 'green', 'dog': '泰迪'},\n",
       " {10, 20, 30}]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#特征3：列表中的元素可以是任意的对象\n",
    "list_1 = [2,2.5,\"abc\",True,False,[1,2,3],(2,4,6),{\"dog\":\"泰迪\",\"color\":\"green\"},{10,20,30}]\n",
    "list_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表长度\n",
    "len(list_1)  #得出列表中有多少个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 访问列表元素——索引\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lucy\n",
      "klaus\n",
      "My name is jack.\n"
     ]
    }
   ],
   "source": [
    "name = [\"jack\",\"lucy\",\"klaus\"]\n",
    "print(name[1])   #打印出name中第二个元素\n",
    "print(name[-1]) #打印出name中最后一个元素\n",
    "print(\"My name is \" + name[0] + \".\")   #打印出一段话，由字符串+列表元素+句号组成"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取列表的一部分——切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 切片的形式由方括号[]和start：stop：step组成\n",
    "+ 取值上是左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[75, 20, 30, 120, 50, 40, 80, 100]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number = [75,20,30,120,50,40,80,100]\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[80]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[6:7]  # 1代表第2个数即20,3代表第4个数即120，而1:3，左边1能取到，右边3取不到，所以实际上是取了1:2，即取列表中的第二个数到第三个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[30, 120, 50, 40, 80, 100]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[2:]  #  2代表的是第三个数即30，[2:]即表示从第三个数一直取到最后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[40, 80, 100]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[-3:]   # -3代表倒数第三个数即40，从倒数第三个数取到最后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[75, 30, 50, 80]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[0:7:2]  #开始值是第一个数即75，终值是第8个数即100，步长为2，按照这样来取数，可以理解为隔一个数来取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[75, 20, 30, 120, 50, 40, 80, 100]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[:]    #复制一个number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 80, 40, 50, 120, 30, 20, 75]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number[::-1]   #得到一个反序的number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表增删改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple', 'banana', 'pear']"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建一个水果列表\n",
    "fruit = [\"apple\",\"banana\",\"pear\"]    \n",
    "fruit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表最后增加一个元素 append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple', 'pomelo', 'banana', 'pear', 'grape', 'watermelon', 'strawberry']"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruit.append(\"strawberry\")\n",
    "fruit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表最后增加一个列表append-extend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'pear', 'strawberry', 'strawberry', ['orange', 'grape']]\n"
     ]
    }
   ],
   "source": [
    "fruit.append([\"orange\",\"grape\"])\n",
    "print(fruit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ append (追加)在列表后追加内容\n",
    "+ extend (扩展)在列表最后添加一个列表，并使其中的元素<font color=red>完全融入原列表</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple', 'banana', 'pear', 'grape', 'watermelon']"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruit = [\"apple\",\"banana\",\"pear\"]    \n",
    "fruit.extend([\"grape\",\"watermelon\"])\n",
    "fruit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表任意位置插入元素insert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'pomelo', 'banana', 'pear', 'grape', 'watermelon']\n"
     ]
    }
   ],
   "source": [
    "fruit.insert(1,\"pomelo\")\n",
    "print(fruit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53\n"
     ]
    }
   ],
   "source": [
    "#返回一个(0-99)随机数\n",
    "print(random.choice(range(100)))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 注意，这个很重要"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如在爬虫开发中，常常使用随机的浏览器类型，不同IP，不同代理等进行随机访问目标网站。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(random.choice([1, 2, 3, 5, 9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l\n"
     ]
    }
   ],
   "source": [
    "print(random.choice('Hello World'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20, 16, 5, 10]\n"
     ]
    }
   ],
   "source": [
    "ls1 = [20, 16, 10, 5];\n",
    "random.shuffle(ls1) #返回重新洗牌列表，随机\n",
    "print(ls1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## join"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- join把数组元素串起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "List1 = ['a','','c','dddddd'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'acdddddd'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\".join(List1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "常用集合类型",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
